<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2019/12/27
 * Time: 9:55
 */
namespace App\Common\Utils;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class BaseRepository
{
    protected $model;

    // 获取单条
    public function get($id, $keyName = 'id',$columns = ['*'],$whereRaw = FALSE){
        if ( $keyName === FALSE ) {
            $where = $id;
        } else {
            $where = [
                $keyName => $id,
            ];
        }
        if(!$whereRaw){
            return $this->model->where($where)->get($columns)->first();
        }else{
            return $this->model->where($where)->whereRaw($whereRaw)->get($columns)->first();
        }
    }

    // 获取列表
    public function getList($filters=[], $sorts =[], $page=1, $columns=['*'], $pageSize=10, $pageName='page'){
        $result = $this->model;
        if(!empty($sorts)){
            foreach ($sorts as $key=>$val){
                $result = $result->orderBy($key, $val);
            }
        }
        return $result->where($filters)->paginate($pageSize,$columns,$pageName,$page);
    }

    /**
     * 获取分页数据
     */
    public function getPageList($filters=[], $columns=['*'],$sorts =['created_at'=>'desc'], $pageSize=10)
    {
        $result = $this->model;
        if(!empty($sorts)){
            foreach ($sorts as $key=>$val){
                $result = $result->orderBy($key, $val);
            }
        }
        return $result->where($filters)->paginate($pageSize,$columns);
    }

    // 获取所有
    public function getAll($filters=[], $sorts =[], $columns=['*'],$whereRaw = FALSE){
        $result = $this->model;
        if(!empty($sorts)){
            foreach ($sorts as $key=>$val){
                $result = $result->orderBy($key, $val);
            }
        }
        if(!$whereRaw){
            return $result->where($filters)->get($columns);
        }else{
            return $result->where($filters)->whereRaw($whereRaw)->get($columns);
        }
    }

    // 新增
    public function add($data){
        $recode = $this->model->create($data);
        return $recode->id;
    }

    // 新增
    public function addAll(array $data){
        $this->model->insert($data);
    }

    // 修改
    public function edit($data, $id ,$keyName = 'id',$whereRaw = FALSE){
        if ( $keyName === FALSE ) {
            $where = $id;
        } else {
            $where = [
                $keyName => $id,
            ];
        }
        if(!$whereRaw){
            return $this->model->where($where)->update($data);
        }else{
            return $this->model->where($where)->whereRaw($whereRaw)->update($data);
        }
    }

    /**
     * 更新或者修改
     * @param $data array 更新的条件
     * @param $id array 需要更新的数据
     */
    public function saveOrInsert($where=[],$data=[])
    {
        if(empty($where) || empty($data)){
            return false;
        }
        return $this->model->updateOr($where,$data);
    }

    /**
     * 更新或者修改 (返回模型)
     * @param $data array 更新的条件
     * @param $id array 需要更新的数据
     */
    public function saveOrCreate($where,$data)
    {      
        if(empty($where) || empty($data)){
            return false;
        }
        return $this->model->updateOrCreate($where,$data);
    }

    // 删除
    public function delete($id ,$keyName = 'id'){
        if ( $keyName === FALSE ) {
            $where = $id;
        } else {
            $where = [
                $keyName => $id,
            ];
        }
        $this->model->where($where)->delete();
    }

    //字段自增
    public function increment($where=[],$field='view_num',$sum=1)
    {
        if(empty($where)) return false;
        return $this->model->where($where)->increment($field,$sum);
    }

    //字段自减
    public function decrement($where=[],$field='view_num',$sum=1)
    {
        if(empty($where)) return false;
        return $this->model->where($where)->decrement($field,$sum);
    }

    public function runQrySql($sql){
        return DB::select($sql);
    }

    public function count($where=[])
    {
        return $this->model->where($where)->count();
    }
}
